package com.word.template.util;


import com.sushengren.easyword.EasyWord;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @ClassName: GenerateUtils
 * @Describe:
 * @Author: shendong
 * @LastModified:
 * @Version: V1.0
 */
@Slf4j
public class GenerateUtils {

    /**
     *
     * @param bean  参数对象
     * @param inputStream  模版
     * @return
     */
    @SneakyThrows
    public static byte[] generateParamBill(Object bean,InputStream inputStream) {
        // 2.填充数据
        EasyWord.Builder builder = EasyWord.of(inputStream).doWrite(bean);
        //3.判断是提交还是预览
        return submit(builder);

    }


    private static byte[] submit(EasyWord.Builder builder) throws IOException {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        // 输出docx文件
        builder.toOutputStream(outputStream);
        // 将docx转换为pdf
        byte[] bytes = Word2PDF.doc2pdf(outputStream);
        // 1、获取流bytes
        return bytes;
    }

    public static void writeBytesToFile(byte[] data, String filePath) throws IOException {
        try (FileOutputStream fos = new FileOutputStream(filePath)) {
            fos.write(data);
        }
    }

}
